#include "SpaceInvadersPCH.h"
#include "DrawContext.h"

void DrawContext::DrawModel(Data_RenderData::RenderData* pRenderData, cr::uint32_t idxModel) const
{
	Data_RenderData::Model* pModel = pRenderData->m_pModels + idxModel;
	Data_RenderData::ModelLod* pLod = pRenderData->m_pModelLods+pModel->m_idxFirstLod;
	for (cr::uint32_t iLod = 0; iLod < pModel->m_numLods; ++iLod, ++pLod)	
	{
		Data_RenderData::Submodel* pSubmodel = pRenderData->m_pSubmodels+pLod->m_idxFirstSubModel;
		for (cr::uint32_t iSubmodel = 0; iSubmodel < pLod->m_numSubModels; ++iSubmodel, ++pSubmodel)		
		{
			gk::RenderBindingHandle hRenderBinding = (gk::RenderBinding*)pSubmodel->m_runtimeData;
			gk::BufferHandle hIndexBuffer = (gk::Buffer*)(pRenderData->m_pBuffers + pSubmodel->m_idxIndexBuffer)->m_runtimeData;
			Data_RenderData::Material* pMaterial = pRenderData->m_pMaterials + pSubmodel->m_idxMaterial;
			Material* pRuntimeMaterial = (Material*)pMaterial->m_runtimeData;
			RenderMethod& renderMethod = pRuntimeMaterial->GetRenderMethod(spaceinvaders::prehash::hs_Default);

			m_hContext->SetRenderBinding(hRenderBinding);
			gp::ParameterBlockContainer* pContainer = (gp::ParameterBlockContainer*)(&m_parameters);
			gp::ParameterBlockHelpers::ApplyParameterBlocks(renderMethod.GetConstantBufferSet(), renderMethod.GetGpuProgram(), pContainer);
			renderMethod.GetConstantBufferSet()->Commit(m_hContext);
			m_hContext->DrawIndexedPrimitives(hIndexBuffer, gk::PrimitiveType::TriangleList, pSubmodel->m_numIndices, pSubmodel->m_firstIndex, pSubmodel->m_numVertices, pSubmodel->m_firstVertex);
			m_hContext->SetRenderBinding(NULL);
		}
	}
}